/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Shop;



import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author ThanhTam
 */
public class ListLine implements Serializable {

    //<editor-fold defaultstate="collapsed" desc="Variables">
    
    private ArrayList<LineProduct> listLine;
    private Connection conn;
    private Statement stm;
    private PreparedStatement pstm;
    private ResultSet rs;
    private boolean stateList = true;
    
     public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }
    // </editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Constructor">
    public ListLine() {
        listLine = new ArrayList<LineProduct>();

    }
    // </editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Method">
    public ArrayList<LineProduct> getListLine() throws SQLException {
        refreshList();
        return listLine;
    }

    public int getItemCount() throws SQLException {
        refreshList();
        return listLine.size();
    }

    public void AddLine(LineProduct lp) throws SQLException {
        if (!listLine.contains(lp)) {
            try {
                pstm = conn.prepareStatement(
                        "INSERT INTO tbldongsanpham ('fldMaDong','fldTenDong','fldMaHang','fldMaSanPham') VALUES (?,?,?,?)");
                pstm.setString(2, lp.getNameLine());
                pstm.setInt(1, lp.getIdLine());
                pstm.setInt(3, lp.getIdCompany());
                pstm.setInt(4, lp.getIdType());
                pstm.executeUpdate();
            } catch (SQLException ex) {
                System.err.println("Can't add item of ListLine in to Database by error : \n" + ex.getMessage());
            } finally {
                stm.close();
                rs.close();
                stateList = true;
            }
        }
    }

    public void DelLine(LineProduct lp) throws SQLException {
        if (this.getListLine().contains(lp)) {
            try {
                pstm = conn.prepareStatement(
                        "DELETE FROM tbldongsanpham WHERE fldMaDong=?");
                pstm.setInt(1, lp.getIdLine());
                pstm.executeUpdate();
            } catch (SQLException ex) {
                System.err.println("Can't add item of ListLine in to Database by error : \n" + ex.getMessage());
            } finally {
                stm.close();
                rs.close();
                stateList = true;
            }
        }
    }

    private void refreshList() throws SQLException {
        if (stateList) {
            try {
                stm = conn.createStatement();
                rs = stm.executeQuery("SELECT * FROM tbldongsanpham");
                while (rs.next()) {
                    listLine.add(new LineProduct(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getInt(4)));
                }
            } catch (Exception ex) {
                System.err.println("Get list line not success");
            } finally {
                stm.close();
                rs.close();
                stateList = false;
            }
        }
    }
    
    // </editor-fold>
}
